package mcommand;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;

/**
 * Defines where the missile bay should be and how the number of left rockets
 * should be handled and showed on the board.
 *
 * @author Marcel, Christian, Klaus
 */
public class MissileBay {
    // path constants to image files
    public static final String PATH_IMAGE_FLAMELEFT = "assets/flame_left.png";
    public static final String PATH_IMAGE_FLAMERIGHT = "assets/flame_right.png";

    // initiates the Image variables for the missileBay
    protected static Image missileBay;
    protected static Image flameLeft;
    protected static Image flameRight;

    // animation values to animate destroyed missileBays
    protected static final int ANIMATION_SPEED = 5;
    protected int animationProgress = 0;
    
    // false = leftFlame, true = rightFlame
    protected boolean animationSwitch = false;

    // initiates the position variables
    private int pos_x;
    private int pos_y;

    //defines the rectangle size
    private static final int RECTANGLE_WIDTH = 20;
    private static final int RECTANGLE_HEIGHT = 7;

    // initiates the label position variables
    private int label_pos_x;
    private int label_pos_y;
    // initiates the rocketCount variable
    private int rocketCount;

    private MissileBayState mbs;

    /**
     * Sets the position values to the beginning tries to load the flames for
     * the destroyed missile bay sets the missile bay state for the beginning to
     * ALIVE.
     * 
     * @param pos_x pos_x
     * @param pos_y pos_y
     */
    public MissileBay(final int pos_x, final int pos_y) {
        // sets the position values
        this.pos_x = pos_x;
        this.pos_y = pos_y;

        if (flameLeft == null) {
            // tries to load into the variable flameLeft the image for the left
            // flame
            try {
                ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        PATH_IMAGE_FLAMELEFT));
                flameLeft = ii.getImage();
                // if something went wrong a message is printed out on the
                // console
            } catch (Exception e) {
                System.err.println("Image Flame Left could not be loaded!");
            }
        }

        if (flameRight == null) {
            // tries to load into the variable flameRight the image for the
            // right flame
            try {
                ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        PATH_IMAGE_FLAMERIGHT));
                flameRight = ii.getImage();
                // if something went wrong a message is printed out on the
                // console
            } catch (Exception e) {
                System.err.println("Image Flame Right could not be loaded!");
            }
        }
        // sets the missile state for the beginning to alive
        mbs = MissileBayState.ALIVE;
    }

    /**
     * Sets the position values if they must be changed.
     * 
     * @param x x
     * @param y y
     */
    public final void setPosition(final int x, final int y) {
        pos_x = x;
        pos_y = y;
    }

    /**
     * Returns the x position.
     * 
     * @return x
     */
    public final int getX() {
        return pos_x;
    }

    /**
     * Returns the y position.
     * 
     * @return y
     */
    public final int getY() {
        return pos_y;
    }

    /**
     * Sets the label position.
     * 
     * @param x x
     * @param y y
     */
    public final void setLabelPosition(final int x, final int y) {
        label_pos_x = x;
        label_pos_y = y;
    }

    /**
     * Sets the number of rockets that the missile bay has left.
     * 
     * @param rocketCount rocketCount
     */
    public final void setRocketCount(final int rocketCount) {
        this.rocketCount = rocketCount;
    }

    /**
     * Returns how many rockets the missile base has left.
     * 
     * @return number of rockets available
     */
    public final int getRocketCount() {
        return rocketCount;
    }

    /**
     * Returns false if the missile bay has no more rockets if it has one or
     * more rockets, the rockets decreases of one and true is returned.
     * 
     * @return true or false
     */
    public final boolean decreaseRocketCount() {
        if (rocketCount < 1) {
            return false;
        } else {
            rocketCount--;

            return true;
        }
    }

    /**
     * Changes every 6 update cycles the orientation of the flame (left or
     * right).
     */
    public final void update() {
        if (!isAlive()) {
            // adds to the animationProgress 1
            animationProgress++;
            // if the animationProgress is bigger than the ANIMATION_SPEED
            if (animationProgress > ANIMATION_SPEED) {
                // animationProgress is 0 again
                animationProgress = 0;
                // animationSwitch changes if false to true, or if true to false
                animationSwitch = !animationSwitch;
            }
        }
    }

    /**
     * Draws how many rockets the missile bay has left draws the flame with
     * orientation left or right when variable depends on values of
     * animationSwitch (true or false).
     * 
     * @param g2d g2d
     */
    public final void paint(final Graphics2D g2d) {
        // if there are more than 0 rockets
        if (getRocketCount() > 0) {
            // draws the rocket number
            g2d.drawString("" + this.getRocketCount(),
                    label_pos_x, label_pos_y);
        }
        // if the city is not alive anymore
        if (!isAlive()) {
            // draws flame with left orientation if animationSwitch is true
            if (animationSwitch) {
                g2d.drawImage(
                        flameLeft,
                        getX() - (flameLeft.getWidth(null) / 2),
                        (getY() - flameLeft.getHeight(null) + RECTANGLE_HEIGHT),
                        null);
            } else {
                g2d.drawImage(
                        flameRight,
                        getX() - (flameRight.getWidth(null) / 2),
                        (getY() - flameRight.getHeight(null)
                                + RECTANGLE_HEIGHT),
                        null);
            }
        }
    }

    /**
     * Returns a new rectangle that is some pixel bigger than the MissileBay
     * itself. (for collision handling)
     * 
     * @return new rectangle
     */
    public final Rectangle2D getRectangle() {
        return new Rectangle2D.Float(getX() - (RECTANGLE_WIDTH / 2), getY(),
                RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
    }

    /**
     * Returns true if the missile bay is ALIVE, either false.
     * 
     * @return true or false
     */
    public final boolean isAlive() {
        if (mbs == MissileBayState.ALIVE) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Gets the missile Bay State.
     * 
     * @return mbs mbs
     */
    public final MissileBayState getState() {
        return mbs;
    }

    /**
     * Sets the missile Bay State.
     * 
     * @param mbs mbs
     */
    public final void setState(final MissileBayState mbs) {
        this.mbs = mbs;
    }

    /**
     * Holds the missile bay state (Alive, Destroyed).
     * 
     */
    public enum MissileBayState {
        ALIVE,
        DESTROYED
    }
}
